package com.bootx;

import com.bootx.pojo.FormItem;
import com.bootx.util.WebUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Demo {

    public static void main(String[] args) throws IOException {
        List<FormItem> formItems = new ArrayList<>();
        Document doc = Jsoup.connect("https://www.aixiezuoyu.com/aiWrite/creative/YearStatement/")
                .get();
        Element left_box = doc.getElementsByClass("left_box").first();
        if(left_box!=null){
            Elements children = left_box.children();
            for (int i = 1; i < children.size(); i++) {
                FormItem formItem = new FormItem();
                Element child = children.get(i);
                Element span = child.getElementsByTag("span").first();
                formItem.setLabel(span.text());
                Elements elementsByTag = child.getElementsByTag("input");
                if(elementsByTag.size()>0){
                    formItem.setType(elementsByTag.getFirst().attr("type"));
                    List<String> options = new ArrayList<>();
                    elementsByTag.forEach(input -> {
                        if(formItem.getType().equals("radio") || formItem.getType().equals("checkbox")){
                            String value = input.attr("value");
                            options.add(value);
                        }
                    });
                    formItem.setOptions(options);
                }else{
                    formItem.setType("textarea");
                    // 说明是输入框
                    for (Element textarea : child.getElementsByTag("textarea")) {
                        formItem.setPlaceholder(textarea.attr("placeholder"));
                        formItem.setDefaultValue(textarea.attr("value"));
                        try {
                            formItem.setMax(Integer.valueOf(textarea.attr("maxlength")));
                        }catch (Exception e){}
                    }
                }
                if(!children.isEmpty()){
                    formItems.add(formItem);
                }
            }
        }
    }
}
